package com.zqxx.examine.management.controller.page.admin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.zqxx.examine.common.utils.Page;
import com.zqxx.examine.common.utils.PagingUtil;
import com.zqxx.examine.management.service.QuestionService;
import com.zqxx.examine.management.service.UserService;
import com.zqxx.examine.model.question.Field;
import com.zqxx.examine.model.question.KnowledgePoint;
import com.zqxx.examine.model.question.Tag;
import com.zqxx.examine.model.user.Department;

@Controller
public class CommonPageAdmin
{
  @Autowired
  private QuestionService questionService;
  @Autowired
  private UserService userService;
  
  @RequestMapping(value={"/admin/common/tag-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String tagListPage(Model model, @RequestParam(value="page", required=false, defaultValue="1") int index)
  {
    Page<Tag> page = new Page();
    page.setPageNo(index);
    page.setPageSize(8);
    List<Tag> tagList = this.questionService.getTags(page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/common/tag-list");
    model.addAttribute("tagList", tagList);
    model.addAttribute("pageStr", pageStr);
    return "tag-list";
  }
  
  @RequestMapping(value={"/admin/common/add-tag"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String addTagPage(Model model)
  {
    return "add-tag";
  }
  
  @RequestMapping(value={"/admin/common/field-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String fieldListPage(Model model, @RequestParam(value="page", required=false, defaultValue="1") int index)
  {
    Page<Field> page = new Page();
    page.setPageNo(index);
    page.setPageSize(8);
    List<Field> fieldList = this.questionService.getAllField(page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/common/field-list");
    model.addAttribute("fieldList", fieldList);
    model.addAttribute("pageStr", pageStr);
    return "field-list";
  }
  
  @RequestMapping(value={"/admin/common/add-field"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String addFieldPage(Model model)
  {
    return "add-field";
  }
  
  @RequestMapping(value={"/admin/common/knowledge-list/{fieldId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String knowledgeListPage(Model model, @PathVariable("fieldId") int fieldId, @RequestParam(value="page", required=false, defaultValue="1") int index)
  {
    Page<KnowledgePoint> page = new Page();
    page.setPageNo(index);
    page.setPageSize(8);
    
    List<Field> fieldList = this.questionService.getAllField(null);
    
    List<KnowledgePoint> pointList = this.questionService.getKnowledgePointByFieldId(fieldId, page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/common/knowledge-list/" + fieldId);
    model.addAttribute("pointList", pointList);
    model.addAttribute("fieldList", fieldList);
    model.addAttribute("fieldId", Integer.valueOf(fieldId));
    model.addAttribute("pageStr", pageStr);
    return "knowledge-list";
  }
  
  @RequestMapping(value={"/admin/common/add-point"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String addKnowledgePage(Model model)
  {
    List<Field> fieldList = this.questionService.getAllField(null);
    
    model.addAttribute("fieldList", fieldList);
    return "add-point";
  }
  
  @RequestMapping(value={"/admin/common/dep-list"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  public String depListPage(Model model, @RequestParam(value="page", required=false, defaultValue="1") int index)
  {
    Page<Department> page = new Page();
    page.setPageNo(index);
    page.setPageSize(8);
    List<Department> depList = this.userService.getDepList(page);
    String pageStr = PagingUtil.getPagelink(index, page.getTotalPage(), "", "admin/common/dep-list");
    model.addAttribute("depList", depList);
    model.addAttribute("pageStr", pageStr);
    return "dep-list";
  }
}